package com.example.tianyidemo.controller;
import com.example.tianyidemo.pojo.LoginResponse;
import com.example.tianyidemo.pojo.Result;
import com.example.tianyidemo.pojo.User;
import com.example.tianyidemo.service.UserService;
import com.example.tianyidemo.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
//校验登录信息
@Slf4j
@CrossOrigin//准许跨域请求
@RestController//包含@Controller（三层架构声明控制器）和@ResponseBody（转化json格式）
public class LoginController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
   public Result login(@RequestBody User user){
        log.info("登录：{}",1);
       User u = userService.login(user);
//       登录成功，生成令牌，下发令牌
        if (u != null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id",u.getUserID());
            claims.put("name",u.getUserName());
            claims.put("pwd",u.getUserPwd());
            claims.put("personalprofile",u.getPersonalprofile());
            claims.put("gender",u.getGender());
            claims.put("img",u.getUserImg());
            claims.put("state",u.getUserState());
            claims.put("email",u.getUserEmail());
            claims.put("signature",u.getSignature());
            claims.put("address",u.getAdress());
            String jwt = JwtUtils.generateJwt(claims);
            // 创建并返回LoginResponse对象
            LoginResponse response = new LoginResponse(jwt, u);
            return Result.success(response);
        }

//        登录失败
           return Result.error("错误！");

   }
}
